From 256faff1e827922a497c5d160339cfbfecaa27ab Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Mon, 9 Feb 2015 14:14:27 -0500 Subject: [PATCH] pass features to doctest binary --- src/cargo/ops/cargo_rustc/compilation.rs | 6 +++- src/cargo/ops/cargo_rustc/mod.rs | 5 ++++ src/cargo/ops/cargo_test.rs | 4 +++ tests/test_cargo_test.rs | 37 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 44dbcf4d8..3322732fe 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::dynamic_lib::DynamicLibrary; use std::ffi::CString; use std::old_path::BytesContainer; @@ -42,6 +42,9 @@ pub struct Compilation { /// Top-level package that was compiled pub package: Package, + + /// Features enabled during this compilation. + pub features: HashSet, } impl Compilation { @@ -55,6 +58,7 @@ impl Compilation { binaries: Vec::new(), extra_env: HashMap::new(), package: pkg.clone(), + features: HashSet::new(), } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 4271d8cfb..d94168a16 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -189,6 +189,11 @@ pub fn compile_targets<'a, 'b>(env: &str, let out_dir = cx.layout(pkg, Kind::Target).build_out(pkg) .display().to_string(); cx.compilation.extra_env.insert("OUT_DIR".to_string(), Some(out_dir)); + + if let Some(feats) = cx.resolve.features(pkg.package_id()) { + cx.compilation.features.extend(feats.iter().cloned()); + } + for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() { let any_dylib = output.library_links.iter().any(|l| { !l.ends_with(":static") && !l.ends_with(":framework") diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index bc18f58e2..2c4ed04ac 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -72,6 +72,10 @@ pub fn run_tests(manifest_path: &Path, p = p.arg("--test-args").arg(test_args.connect(" ")); } + for feat in compile.features.iter() { + p = p.arg("--cfg").arg(format!("feature=\"{}\"", feat)); + } + for (pkg, libs) in compile.libraries.iter() { for lib in libs.iter() { let mut arg = pkg.name().as_bytes().to_vec(); diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 8cd266bd9..715e9875d 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -1352,3 +1352,40 @@ no example target named `foo` to run no bin target named `foo` to run ")); }); + +test!(doctest_feature { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + [features] + bar = [] + "#) + .file("src/lib.rs", r#" + /// ```rust + /// assert_eq!(foo::foo(), 1); + /// ``` + #[cfg(feature = "bar")] + pub fn foo() -> i32 { 1 } + "#); + + assert_that(p.cargo_process("test").arg("--features").arg("bar"), + execs().with_status(0).with_stdout(format!("\ +{compiling} foo [..] +{running} target[..]foo[..] + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured + +{doctest} foo + +running 1 test +test foo_0 ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured + +", compiling = COMPILING, running = RUNNING, doctest = DOCTEST))) +}); -- 2.30.2